﻿using System;
using System.ComponentModel;
using RaytracerLib.MathLib;

namespace Raytracer.PropertyGridDecorators
{
    public class AABBProperties
    {
        public readonly Action<AABB> Setter;
        public readonly Func<AABB> Getter;

        public AABBProperties(Func<AABB> a_getter, Action<AABB> a_setter)
        {
            Getter = a_getter;
            Setter = a_setter;
        }

        [TypeConverter(typeof(Vector3Converter))]
        public Vector3Properties Minimum
        {
            get
            {
                return new Vector3Properties(() => Getter().Minimum, 
                    (v) => Setter(new AABB(v, Getter().Maximum)));
            }
            set
            {
                Setter(new AABB(value.ToVector3(), Getter().Maximum));
            }
        }

        [TypeConverter(typeof(Vector3Converter))]
        public Vector3Properties Maximum
        {
            get
            {
                return new Vector3Properties(() => Getter().Maximum,
                    (v) => Setter(new AABB(Getter().Minimum, v)));
            }
            set
            {
                Setter(new AABB(Getter().Minimum, value.ToVector3()));
            }
        }

        public AABB ToAABB()
        {
            return new AABB(Minimum.ToVector3(), Maximum.ToVector3());
        }
    }

    public class AABBReadonlyProperties
    {
        public readonly AABB AABB;

        public AABBReadonlyProperties(AABB a_aabb)
        {
            AABB = a_aabb;
        }

        [TypeConverter(typeof(Vector3Converter))]
        public Vector3ReadonlyProperties Minimum
        {
            get
            {
                return new Vector3ReadonlyProperties(AABB.Minimum);
            }
        }

        [TypeConverter(typeof(Vector3Converter))]
        public Vector3ReadonlyProperties Maximum
        {
            get
            {
                return new Vector3ReadonlyProperties(AABB.Maximum);
            }
        }
    }
}